package Stream流.Text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;


public class Text1 {
    public static void main(String[] args) {
        /**
         * 练习:
         * 创建一个ArrayList集合，并添加以下字符串，字符串中前面是姓名，后面是年龄
         * "zhangsan,23"
         * "lisi,24"
         * wangwu , 25"
         * 保留年龄大于等于24岁的人，并将结果收集到Map集合中，姓名为键，年龄为值*/

        ArrayList<String> lines = new ArrayList<String>();
        Collections.addAll(lines,"zangsan,23","lisi,24","wangwu,25");
        System.out.println(lines);
        //保留年龄大于等于24的人
        Map<String, Integer> collect = lines.stream().filter(s -> Integer.parseInt(s.split(",")[1]) >= 24).collect(Collectors.toMap(
                new Function<String, String>() {
                    //将结果收集到Map集合中，姓名为键，年龄为值
                    @Override
                    public String apply(String s) {
                        return s.split(",")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split(",")[1]);
                    }
                }));

            //简化版
        Map<String, Integer> collect1 = lines.stream().filter(line -> Integer.parseInt(line.split(",")[1]) >= 24)
                .collect(Collectors.toMap(s -> s.split(",")[0],
                        s -> Integer.parseInt(s.split(",")[1])));

        System.out.println(collect);

        System.out.println("================================");
        //通过键找值
        Set<Map.Entry<String, Integer>> entries = collect.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        System.out.println("================================");
        collect.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println(s+"--->"+integer);
            }
        });
        System.out.println("================================");
    }
}
